package cn.tataben.sharding.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Response {

    private String msg = "";
    private String code = "";

    public Response() {
        this.msg = Constant.SUCCESS[1];
        this.code = Constant.SUCCESS[0];
    }

    public Response(String[] err) {
        fill(err);
    }

    public Response fill(String[] err) {
        if(err != null && err.length == 2){
            this.msg = err[1];
            this.code = err[0];
        } else {
            this.msg = Constant.UNKNOWN[1];
            this.code = Constant.UNKNOWN[0];
        }
        return this;
    }

    public String toJsonString() throws JsonProcessingException{
        return new ObjectMapper().writeValueAsString(this);
    }

}